﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pb;
public class DNGameHall : MonoBehaviour
{
    EginUser user = EginUser.Instance;
    public GameObject Root;
    public GameObject table;
    public Transform grid;
    public GameObject closebtn;
    public GameObject DNHall_UI;
    public GameObject kaifangjieshao_UI;
    public GameObject NullBtn;
    public GameObject HaveBtn;
    public GameObject AddRoomBtn;
    private List<int> _roomlist = new List<int>();
    private static DNGameHall _instance;
    private int _page = 1;


    public static DNGameHall Instance
    {
        get
        {
            return _instance;
        }
    }

    void Awake()
    {
        _instance = this;
        UIEventListener.Get(closebtn).onClick = ButtonClick;
        UIEventListener.Get(NullBtn).onClick = ButtonClick;
        UIEventListener.Get(HaveBtn).onClick = ButtonClick;
        UIEventListener.Get(AddRoomBtn).onClick = ButtonClick;
        table.SetActive(false);
    }

    public void Init(NnRoomListResponse resp)
    {

        DNHall_UI.SetActive(true);
        kaifangjieshao_UI.SetActive(false);

        if (resp.Status == STATUS.Success)
        {
            if (NNCoinJoinRoom.Instance._isChange == true)
            {
                for (int i = 0; i < grid.childCount; i++)
                {
                    Destroy(grid.GetChild(i).gameObject);
                }
                _roomlist.Clear();
                NNCoinJoinRoom.Instance.ResetChange();

            }
           
        
            try
            {
                Destroy(grid.transform.Find("Create").gameObject);
                Destroy(grid.transform.Find("more").gameObject);
            }
            catch (System.Exception)
            {

                Debug.Log("---第一次打开DN大厅--");
            }

            for (int i = 0; i < resp.Rooms.Count; i++)
            {
                if (!_roomlist.Contains(resp.Rooms[i].RoomId))
                {
                    GameObject obj = GameObject.Instantiate(table);
                    obj.name = "Table";
                    obj.transform.Find("Haveroom/roomid").GetComponent<UILabel>().text = resp.Rooms[i].RoomId.ToString();
                    obj.transform.Find("Haveroom").gameObject.SetActive(true);
                    obj.transform.Find("NoRoom").gameObject.SetActive(false);
                    obj.transform.Find("Haveroom/DIzhu").GetComponent<UILabel>().text = resp.Rooms[i].Score.ToString();
                    for (int j = 0; j < resp.Rooms[i].MemberCount; j++)
                    {
                        obj.transform.Find("User/User" + j).GetComponent<UISprite>().spriteName = resp.Rooms[i].Members[j].AvatarId.ToString();
                    }
                    obj.transform.Find("Haveroom/touixang").GetComponent<UISprite>().spriteName = resp.Rooms[i].Master.AvatarId.ToString();
                    obj.SetActive(true);
                    obj.transform.SetParent(grid);
                    obj.transform.localPosition = Vector3.zero;
                    obj.transform.localRotation = Quaternion.identity;
                    obj.transform.localScale = Vector3.one;
                    UIEventListener.Get(obj).onClick = ButtonClick;
                    _roomlist.Add(resp.Rooms[i].RoomId);

                }
            }
            GameObject objs = GameObject.Instantiate(table);
            objs.name = "Create";
            objs.transform.Find("Haveroom").gameObject.SetActive(false);
            objs.transform.Find("NoRoom").gameObject.SetActive(true);
            objs.SetActive(true);
            objs.transform.SetParent(grid);
            objs.transform.localPosition = Vector3.zero;
            objs.transform.localRotation = Quaternion.identity;
            objs.transform.localScale = Vector3.one;
            UIEventListener.Get(objs).onClick = ButtonClick;

            GameObject _obj = GameObject.Instantiate(table);
            _obj.name = "more";
            _obj.transform.Find("Haveroom").gameObject.SetActive(false);
            _obj.transform.Find("NoRoom").gameObject.SetActive(false);
            _obj.transform.Find("more").gameObject.SetActive(true);
            _obj.SetActive(true);
            _obj.transform.SetParent(grid);
            _obj.transform.localPosition = Vector3.zero;
            _obj.transform.localRotation = Quaternion.identity;
            _obj.transform.localScale = Vector3.one;
            UIEventListener.Get(_obj).onClick = ButtonClick;

            grid.transform.GetComponent<UIGrid>().enabled = true;
        }
        else
        {
            user.logMsg = resp.Msg;
            GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));
        }
    }
    private int _isEnter = 0; //1表示不进入；2表示进入
    private void ButtonClick(GameObject go)
    {
        if (go.name == "close")
        {
            _page = 1;
            for (int i = 0; i < grid.childCount; i++)
            {
                Destroy(grid.GetChild(i).gameObject);
            }
            _roomlist.Clear();
            gameObject.SetActive(false);
        }
        else if (go.name == "Create")
        {
            //默认不进去
            _isEnter = 1;
            NullBtn.transform.Find("Background").GetComponent<UISprite>().spriteName = "xz4";
            HaveBtn.transform.Find("Background").GetComponent<UISprite>().spriteName = "xz3";
            kaifangjieshao_UI.SetActive(true);
            DNHall_UI.SetActive(false);


        }
        else if (go.name == "Table")
        {
            NnEnterRoomRequest req = new NnEnterRoomRequest();
            req.RoomId = int.Parse(go.transform.Find("Haveroom/roomid").GetComponent<UILabel>().text);
            SocketManager socketManager = SocketManager.Instance;
            socketManager.SendProtobuf1(req, MsgId.NnEnterRoomRequest);
        }
        else if (go.name == "NULLBtn")
        {
            if (go.transform.Find("Background").GetComponent<UISprite>().spriteName == "xz3")
            {
                //不进入
                go.transform.Find("Background").GetComponent<UISprite>().spriteName = "xz4";
                HaveBtn.transform.Find("Background").GetComponent<UISprite>().spriteName = "xz3";
                _isEnter = 1;
            }
            else
            {//进入
                go.transform.Find("Background").GetComponent<UISprite>().spriteName = "xz3";
                HaveBtn.transform.Find("Background").GetComponent<UISprite>().spriteName = "xz4";
                _isEnter = 2;
            }

        }
        else if (go.name == "Have_Btn")
        {
            if (go.transform.Find("Background").GetComponent<UISprite>().spriteName == "xz3")
            {
                //进入
                go.transform.Find("Background").GetComponent<UISprite>().spriteName = "xz4";
                NullBtn.transform.Find("Background").GetComponent<UISprite>().spriteName = "xz3";
                _isEnter = 2;
            }
            else
            {//不进入
                go.transform.Find("Background").GetComponent<UISprite>().spriteName = "xz3";
                NullBtn.transform.Find("Background").GetComponent<UISprite>().spriteName = "xz4";
                _isEnter = 1;
            }
        }
        else if (go.name == "AddRoomBtn")
        {
            NnCreateRoomRequest req = new NnCreateRoomRequest();
            req.GroundType = NN_GROUND_TYPE.Hall;
            req.RoomType = NNCoinJoinRoom.Instance._current_type;
            if (_isEnter == 1)
            {
                req.IsEmpty = true;
            }
            else if (_isEnter == 2)
            {
                req.IsEmpty = false;
            }
            SocketManager socketManager = SocketManager.Instance;
            socketManager.SendProtobuf1(req, MsgId.NnCreateRoomRequest);
        }
        else if (go.name == "more")
        {
            //type 0是阿富汗；1是中国； 2是迪拜
            NnRoomListRequest req = new NnRoomListRequest();
            req.GroundType = NN_GROUND_TYPE.Hall;
            _page += 1;
            req.Page = _page;
            req.RoomType = NNCoinJoinRoom.Instance._current_type;
            Debug.LogError("-GroundType=" + req.GroundType + "-RoomType=" + req.RoomType + "-Page=" + req.Page);
            SocketManager socketManager = SocketManager.Instance;
            socketManager.SendProtobuf1(req, MsgId.NnRoomListRequest);
        }
    }



}
